package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.constants.Constants;
import com.easychat.entity.config.AppConfig;
import com.easychat.entity.dto.SysSettingDTO;
import com.easychat.entity.po.UserInfoBeauty;
import com.easychat.entity.query.UserInfoBeautyQuery;
import com.easychat.entity.vo.PaginationResultVO;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.redis.RedisComponent;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController("adminSettingController")
@RequestMapping("/admin")
public class AdminSettingController extends ABaseController{

    @Resource
    private RedisComponent redisComponent;
    @Resource
    private AppConfig appConfig;

    /**
     * 获取系统设置
     * @return
     */
    @RequestMapping("/getSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO getSysSetting() {
        SysSettingDTO sysSetting = redisComponent.getSysSetting();
        return getSuccessResponseVO(sysSetting);
    }

    /**
     * 保存系统设置
     * @param sysSettingDTO 系统设置
     * @param robotFile 机器人头像
     * @param robotCover 机器人头像缩略图
     * @return
     */
    @RequestMapping("/saveSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO saveSysSetting(
            SysSettingDTO sysSettingDTO,
            MultipartFile robotFile,
            MultipartFile robotCover
    ) throws IOException {
        if (robotFile != null) {
            //将图像保存本地
            String projectFolderPath = appConfig.getProjectFolder();
            String fileFolderPath = projectFolderPath + Constants.FILE_FOLDER_FILE;
            File avatarFolder = new File(fileFolderPath + Constants.FILE_FOLDER_AVATAR);
            if (!avatarFolder.exists()){
                avatarFolder.mkdirs();
            }
            File imageFile = new File(avatarFolder.getPath() + "/" + Constants.ROBOT_UID + Constants.IMAGE_EXTENSION);
            File coverFile = new File(avatarFolder.getPath() + "/" + Constants.ROBOT_UID + Constants.COVER_IMAGE_EXTENSION);
            robotFile.transferTo(imageFile);
            robotCover.transferTo(coverFile);
        }
        redisComponent.saveSysSetting(sysSettingDTO);
        return getSuccessResponseVO(null);
    }
}
